
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';


void main() {
  runApp(CanvasDemo());
}

class CanvasDemo extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CanvasMethodDemo(),
    );
  }
}

class CanvasMethodDemo extends StatefulWidget {

  @override
  State<CanvasMethodDemo> createState() => _CanvasMethodDemoState();
}

class _CanvasMethodDemoState extends State<CanvasMethodDemo> {

  double statusBarHeight = MediaQueryData.fromWindow(window).padding.top;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomPaint(
        painter: Sky(),
        child: const Center(
          // child: Text(
          //   'Once upon a time...',
          //   style: TextStyle(
          //     fontSize: 40.0,
          //     fontWeight: FontWeight.w900,
          //     color: Color(0xFFFFFFFF),
          //   ),
          // ),
        ),
      ),
    );
  }
}

class Sky extends CustomPainter {

  @override
  void paint(Canvas canvas, Size size) {
    final Rect rect = Offset.zero & size;
    const RadialGradient gradient = RadialGradient(
      center: Alignment(0.7, -0.6),
      radius: 0.2,
      colors: <Color>[Color(0xFFFFFF00), Color(0xFF0099FF)],
      stops: <double>[0.4, 1.0],
    );
    canvas.drawRect(
      rect,
      Paint()..shader = gradient.createShader(rect),
    );
  }

  @override
  SemanticsBuilderCallback get semanticsBuilder {
    return (Size size) {
      // 用标签“太阳”标注一个包含太阳图片的矩形
      // 当设备上的文本到语音功能启用时
      // 用户将能够通过触摸在这张照片上定位太阳
      Rect rect = Offset.zero & size;
      final double width = size.shortestSide * 0.4;
      rect = const Alignment(0.8, -0.9).inscribe(Size(width, width), rect);
      return <CustomPainterSemantics>[
        CustomPainterSemantics(
          rect: rect,
          properties: const SemanticsProperties(
            label: 'Sun',
            textDirection: TextDirection.ltr,
          ),
        ),
      ];
    };
  }

  // 由于这个天空画家没有领域，它总是画同样的东西
  // 语义信息是相同的
  // 因此我们在这里返回false
  // 如果我们有字段(从构造函数中设置)
  // 那么如果它们中的任何一个不同于oldDelegate上的相同字段
  // 我们将返回true
  @override
  bool shouldRepaint(Sky oldDelegate) => false;

  @override
  bool shouldRebuildSemantics(Sky oldDelegate) => false;
}

// class _CanvasMethodDemoState extends State<CanvasMethodDemo> {
//
//   double statusBarHeight = MediaQueryData.fromWindow(window).padding.top;
//   DotPainter painter = DotPainter(_globalPosition);
//
//   static Offset _globalPosition = Offset(0,0);
//
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       body: Padding(
//           padding: EdgeInsets.only(top: statusBarHeight),
//           child: Listener(
//             child: CustomPaint(
//               size: Size.infinite,
//               painter: painter,
//               foregroundPainter: painter,
//               child: null,
//             ),
//             onPointerMove: (PointerMoveEvent e) => {
//               setState(() {
//                 _globalPosition = e.localPosition;
//               })
//             },
//           )
//       ),
//     );
//   }
// }